package club.samubird.simpledatahandlekit.es.core;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.frameworkset.elasticsearch.boot.BBossESStarter;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.entity.MapRestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author sam
 * @date 2019/10/16 11:38
 * @description 功能描述
 */
@Slf4j
@Component
public class QueryBuilderDemo {

	@Autowired
	private BBossESStarter bBossESStarter;

	public void demo() {
		QueryBuilder queryBuilder = QueryBuilders.boolQuery()
				.must(QueryBuilders.matchPhraseQuery("strTextIk", "是我").slop(2));
		SearchSourceBuilder query = new SearchSourceBuilder().query(queryBuilder);
		String jsonStrBody = query.toString();
		ClientInterface restClient = bBossESStarter.getRestClient();
		MapRestResponse search = restClient.search("str-demo-sam/_search", jsonStrBody);
		log.info(" 返回的结果是 = {}", search);
	}


}
